import socket


try:
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

except socket.error, msg:
	print("Failed to create socket. Error code: " + str(msg[0]) + "Error message: " + str(msg[1]))
	sys.exit(1)

print('Socket created.')	


host = 'www.baidu.com'
port = 80

try:
	remote_ip = socket.gethostbyname(host)

except socket.gaierror:
	print("Host name could not be resolved. Exiting")
	sys.exit(1)

print("Ip adddress of " + host + " is " + remote_ip)

s.connect((remote_ip, port))
print("Socket connected to " + host + " on ip " + remote_ip)

message = "GET / HTTP/1.1\r\n\r\n"

try:
	s.sendall(message)

except socket.error:
	print ("Send failed.")
	sys.exit(1)

print("Message send sucessfully.")

reply = s.recv(4096)
print(reply)

s.close()